package xyz.zxcwxy999.news.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import xyz.zxcwxy999.news.entity.Comment;
import xyz.zxcwxy999.news.entity.News;
import xyz.zxcwxy999.news.entity.User;
import xyz.zxcwxy999.news.service.CommentService;
import xyz.zxcwxy999.news.service.NewsService;
import xyz.zxcwxy999.news.service.UserService;

import java.util.Date;

@Controller
public class MainController {
    @Autowired
    UserService userService;
    @Autowired
    NewsService newsService;
    @Autowired
    CommentService commentService;

    /**
     * 进入主页（测试通过）
     *
     * @return
     */
    @GetMapping
    public String index() {
        return "login";
    }

    /**
     * 进入主页（测试通过）
     *
     * @return
     */
    @GetMapping("/")
    public String index01() {
        return "login";
    }

    /**
     * 进入列表（测试通过）
     *
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(Model model) {
        News[] news = newsService.getAllNews();
        model.addAttribute("news", news);
        return "index";
    }

    /**
     * 登录（测试通过）
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/manager/login")
    public String login(@RequestParam String username, @RequestParam String password) {
        boolean result = userService.login(username, password);
        if (result) {
            return "redirect:/list";
        } else {
            return "redirect:/";
        }

    }


    /**
     * 添加新闻（测试通过）
     *
     * @param title
     * @param message
     * @return
     */
    @GetMapping("manager/addnews")
    public String addNews(@RequestParam String title, @RequestParam String message) {
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(message)) {
            return null;
        }
        News news = new News();
        news.setTitle(title);
        news.setMessage(message);
        news.setDate(new Date());
        boolean result = newsService.addNews(news);
        if (result) {
            return "redirect:/list";
        } else {
            return "redirect:/";
        }
    }

    /**
     * 添加用户（测试通过）
     * @param username
     * @param password
     * @return
     */
    @GetMapping("manager/register")
    public String register(@RequestParam String username, @RequestParam String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return null;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        boolean result = userService.register(user);
        if (result) {
            return "redirect:/list";
        } else {
            return "redirect:/";
        }
    }

    /**
     * 删除新闻（测试通过）
     * @param nid
     * @return
     */
    @DeleteMapping("/manager/delete/{nid}")
    public String addVote(@PathVariable("nid") int nid) {
        boolean result = newsService.deleteNews(nid);
        return "redirect:/list";
    }

    /**
     * 查看某一新闻下的全部评论（测试通过）
     * @param nid
     * @param model
     * @return
     */
    @GetMapping("/manager/comment/{nid}")
    public String listComment(@PathVariable("nid") int nid, Model model) {
        Comment[] comment = commentService.getAllComments(nid);
        News news=newsService.getOne(nid);
        model.addAttribute("title",news.getTitle());
        model.addAttribute("message",news.getMessage());
        model.addAttribute("comment", comment);
        return "comment";
    }

    /**
     * 删除一条评论（测试通过）
     * @param cid
     * @return
     */
    @DeleteMapping("/manager/comment/delete/{cid}")
    public String deleteComment(@PathVariable("cid") int cid) {
        commentService.deleteOneComment(cid);
        return "redirect:/list";
    }
}
